import 'package:hive/hive.dart';

import '../index.dart';

part 'time_capsule.g.dart';

@HiveType(typeId: 3)
class TimeCapsule extends HiveObject {
  @HiveField(0)
  final String id;

  @HiveField(1)
  final String content;

  @HiveField(2)
  final String userId;

  @HiveField(3)
  final String createDate;

  @HiveField(4)
  final String openDate;

  @HiveField(5)
  final String description;

  @HiveField(6)
  final bool isOpen;

  @HiveField(7)
  final String email;

  @HiveField(8)
  final String backupEmail;

  @HiveField(9)
  final String? imagePath;

  TimeCapsule({
    required this.id,
    required this.content,
    required this.userId,
    required this.createDate,
    required this.openDate,
    required this.description,
    required this.isOpen,
    required this.email,
    required this.backupEmail,
    this.imagePath,
  });

  factory TimeCapsule.fromJson(Map<String, dynamic> json) {
    return TimeCapsule(
      id: json['id'] as String,
      content: json['content'] as String,
      userId: json['userId'] as String,
      createDate: json['createDate'] as String,
      openDate: json['openDate'] as String,
      description: json['description'] as String,
      isOpen: json['isOpen'] as bool,
      email: json['email'] as String,
      backupEmail: json['backupEmail'] as String,
      imagePath: json['imagePath'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'content': content,
      'userId': userId,
      'createDate': createDate,
      'openDate': openDate,
      'description': description,
      'isOpen': isOpen,
      'email': email,
      'backupEmail': backupEmail,
      'imagePath': imagePath,
    };
  }

  TimeCapsule copyWith({
    String? id,
    String? content,
    String? userId,
    String? createDate,
    String? openDate,
    String? description,
    bool? isOpen,
    String? email,
    String? backupEmail,
    String? imagePath,
  }) {
    return TimeCapsule(
      id: id ?? this.id,
      content: content ?? this.content,
      userId: userId ?? this.userId,
      createDate: createDate ?? this.createDate,
      openDate: openDate ?? this.openDate,
      description: description ?? this.description,
      isOpen: isOpen ?? this.isOpen,
      email: email ?? this.email,
      backupEmail: backupEmail ?? this.backupEmail,
      imagePath: imagePath ?? this.imagePath,
    );
  }

  @override
  String toString() {
    return 'TimeCapsule{id: $id, content: $content, userId: $userId, createDate: $createDate, openDate: $openDate, description: $description, isOpen: $isOpen, email: $email, backupEmail: $backupEmail, imagePath: $imagePath}';
  }
}

final List<TimeCapsule> sampleTimeCapsules = [
  TimeCapsule(
    id: 'tc1',
    content:
        'Dear future me, I hope you remember this moment of pure happiness. Today I graduated from university and I feel like I can conquer the world. Never forget this feeling of accomplishment and the dreams you have right now.',
    userId: '1',
    createDate: '2024-06-15 14:30:00',
    openDate: '2025-06-15 14:30:00',
    description: 'Graduation Day Memories',
    isOpen: false,
    email: 'kina@example.com',
    backupEmail: 'kina@example.com',
    imagePath: MyImages.time1,
  ),
  TimeCapsule(
    id: 'tc2',
    content:
        'I\'m writing this during a difficult time in my life. I lost my job last month and I\'m feeling lost. But I believe things will get better. Future me, I hope you\'re in a better place now and can look back at this with pride for not giving up.',
    userId: '2',
    createDate: '2024-03-20 22:15:30',
    openDate: '2025-03-20 22:15:30',
    description: 'Through the Storm',
    isOpen: false,
    email: 'mark@example.com',
    backupEmail: 'mark@example.com',
    imagePath: MyImages.time2,
  ),
  TimeCapsule(
    id: 'tc3',
    content:
        'Today I started my own business! It\'s scary but exciting. I want to remember this leap of faith and the courage it took to follow my dreams. Future me, I hope the business is thriving and you remember why you started.',
    userId: '3',
    createDate: '2024-01-10 09:45:12',
    openDate: '2026-01-10 09:45:12',
    description: 'Entrepreneurial Journey Begins',
    isOpen: false,
    email: 'hugh@example.com',
    backupEmail: 'hugh@example.com',
    imagePath: null,
  ),
  TimeCapsule(
    id: 'tc4',
    content:
        'I just moved to a new city and I\'m feeling both excited and nervous. Everything is new and different. I hope by the time I read this, I\'ve made great friends and feel at home here.',
    userId: '1',
    createDate: '2024-08-01 16:20:45',
    openDate: '2025-08-01 16:20:45',
    description: 'New City, New Life',
    isOpen: false,
    email: 'kina@example.com',
    backupEmail: 'kina@example.com',
    imagePath: MyImages.time5,
  ),
  TimeCapsule(
    id: 'tc5',
    content:
        'Today I finally told someone I love them. It was terrifying but liberating. Whatever happens, I\'m proud of myself for being brave enough to be vulnerable.',
    userId: '2',
    createDate: '2024-02-14 19:30:22',
    openDate: '2025-02-14 19:30:22',
    description: 'Love Confession',
    isOpen: false,
    email: 'mark@example.com',
    backupEmail: 'mark@example.com',
    imagePath: MyImages.time3,
  ),
  TimeCapsule(
    id: 'tc6',
    content:
        'I just completed my first marathon! 26.2 miles of pure determination. My legs are killing me but my spirit is soaring. This proves I can achieve anything I set my mind to.',
    userId: '3',
    createDate: '2024-05-12 11:45:33',
    openDate: '2024-11-12 11:45:33',
    description: 'Marathon Achievement',
    isOpen: true,
    email: 'hugh@example.com',
    backupEmail: 'hugh@example.com',
    imagePath: MyImages.time4,
  ),
];

List<TimeCapsule> getAllTimeCapsules() {
  return sampleTimeCapsules;
}

List<TimeCapsule> getTimeCapsulesByUserId(String userId) {
  return sampleTimeCapsules
      .where((capsule) => capsule.userId == userId)
      .toList();
}

List<TimeCapsule> getOpenableTimeCapsules() {
  final now = DateTime.now();
  return sampleTimeCapsules.where((capsule) {
    final openDate = DateTime.parse(capsule.openDate);
    return !capsule.isOpen && now.isAfter(openDate);
  }).toList();
}
